package org.ctsi.rsds.base.rest;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public abstract class ResourceHandler {
	public Object copyObj(Object srcObj,Object targObj){
		if(srcObj == null){
			targObj = null;
		}else{
			Method[] methods = srcObj.getClass().getMethods();
			for(Method m : methods){
				if(m.getName().contains("get") && !m.getName().equals("getClass")){
					Object value = null;
					try {
						value = m.invoke(srcObj);
					} catch (IllegalAccessException e1) {
						continue;
					} catch (IllegalArgumentException e1) {
						continue;
					} catch (InvocationTargetException e1) {
						continue;
					}
					String setMethodName = m.getName().replace("get", "set");
					Method setMethod = null;
					try {
						setMethod = targObj.getClass().getMethod(setMethodName, m.getReturnType());
					} catch (NoSuchMethodException e) {
						continue;
					} catch (SecurityException e) {
						continue;
					}	
					try {
						setMethod.invoke(targObj, value);
					} catch (IllegalAccessException e) {
						continue;
					} catch (IllegalArgumentException e) {
						continue;
					} catch (InvocationTargetException e) {
						continue;
					}
				}
			}
		}
		return targObj;
	}
}
